Introducing C# 9: Native-sized integers
Introduction
What are native-sized integers ? They are designed to be an integer which size is specific to the platform. In other words, an instance of this type must be 32 bits on 32-bit hardwares / operating systems, 64 bits on 64-bit hardwares / operating systems.
The CLR / JIT / MSIL supports the definition and usage of native integers / unsigned integers. Since .NET 4.0’s CLR, it is possible to add / substract an integer from a System.IntPtr / System.UIntPtr, and it is possible to do ==
/ !=
comparisons with other System.IntPtr / System.UIntPtr, but any other comparison operation is prohibited…, i.e. they cannot be compared with >
, >=
etc. to each other, so System.IntPtr / System.UIntPtr remain very basic in the amount of pointer arithmetic.
C# 9 brings what mono has brought before: language support for a native-sized signed and unsigned integer types with nint and nuint keyword. The motivation here is for interop scenarios and for low-level libraries, so might not use them often.
Types nint and nuint are represented by the underlying types System.IntPtr and System.UIntPtr with compiler surfacing additional conversions and operations for those types as native ints.
C# 9 syntax, constants and usage
nint constants are in the range [ int.MinValue
, int.MaxValue
].
nuint constants are in the range [ uint.MinValue
, uint.MaxValue
].
There are no MinValue or MaxValue o nint or nuint because, other than nuint.MinValue, those values cannot be emitted as constants.
Usage samples:
When you add an int to a nint the result is a nint, but if you add a long to an nint the result will be a long. This is because the native depending on the platform could be a 32-bit integer or a 64-bits integer.
You can notice that arrays support native-sized signed type as index, but not lists, example:
Microsoft docs says enum support nint and nuint as base type: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/native-integers#miscellaneous
Unfortunately I’m not able to test it, I have a compiler error for now with the latest release of Roslyn I’am currently using (3.8.0-1.20330.5)
A soon as an update is available for enum and native-integers, I’ll update this post.
Thanks for reading 🙂